package test.src.awt_swing;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import java.awt.*;

public class NewCellTreeRendererDemo {
    JFrame jFrame = new JFrame("测试自定义CellTreeRenderer");

    CellData friends = new CellData("test\\src\\awt_swing\\img\\tree\\我的好友.gif", "我的好友");
    DefaultMutableTreeNode friendsNode = new DefaultMutableTreeNode(friends);

    CellData nongYu = new CellData("test\\src\\awt_swing\\img\\tree\\弄玉.gif", "弄玉");
    DefaultMutableTreeNode nongYuNode = new DefaultMutableTreeNode(nongYu);

    CellData liQingZhao = new CellData("test\\src\\awt_swing\\img\\tree\\李清照.gif", "李清照");
    DefaultMutableTreeNode liQingZhaoNode = new DefaultMutableTreeNode(liQingZhao);

    CellData liBai = new CellData("test\\src\\awt_swing\\img\\tree\\李白.gif", "李白");
    DefaultMutableTreeNode liBaiNode = new DefaultMutableTreeNode(liBai);

    CellData socrates = new CellData("test\\src\\awt_swing\\img\\tree\\苏格拉底.gif", "苏格拉底");
    DefaultMutableTreeNode socratesNode = new DefaultMutableTreeNode(socrates);

    CellData huTou = new CellData("test\\src\\awt_swing\\img\\tree\\虎头.gif", "虎头");
    DefaultMutableTreeNode huTouNode = new DefaultMutableTreeNode(huTou);

    private void init() {
        friendsNode.add(nongYuNode);
        friendsNode.add(liQingZhaoNode);
        friendsNode.add(liBaiNode);
        friendsNode.add(socratesNode);
        friendsNode.add(huTouNode);
        JTree jTree = new JTree(friendsNode);
        jTree.setCellRenderer(new MyCellTreeRenderer());

        jFrame.add(new JScrollPane(jTree));

        jFrame.pack();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new NewCellTreeRendererDemo().init();
    }
}

class MyCellTreeRenderer extends JPanel implements TreeCellRenderer {
    private ImageIcon icon;
    private String name;
    private Color backGround;
    private Color foreGround;

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        CellData cellData = (CellData) node.getUserObject();

        this.icon = cellData.getIcon();
        this.name = cellData.getName();

        backGround = hasFocus ? Color.gray : Color.WHITE;
        foreGround = hasFocus ? Color.orange : Color.BLACK;
        return this;
    }

    @Override
    protected void paintComponent(Graphics g) {
        int imageWidth = icon.getIconWidth();
        int imageHeight = icon.getIconHeight();
        g.setColor(backGround);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(foreGround);
        g.drawImage(icon.getImage(), getWidth()/2 - imageWidth/2, 10, null);

        g.setFont(new Font("SansSerif", Font.BOLD, 18));

        g.drawString(name, getWidth()/2 - name.length() * 10, imageHeight + 30);

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(80, 80);
    }
}

class CellData {
    private ImageIcon icon;
    private String name;

    CellData (String path, String name) {
        this.icon = new ImageIcon(path);
        this.name = name;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public String getName() {
        return name;
    }
}